import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:ok_github/page/login/login_page.dart';
import 'package:ok_github/repo/color_repo.dart';
import 'package:ok_ui/flutter_ok_ui.dart';
import 'package:oktoast/oktoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:ok_github/page/route/route.dart';
import 'utils/sp_utils.dart';

void main() async {
  await config();
  runApp(MyApp());
}

Future<void> config() async {
  sp = await SharedPreferences.getInstance();
  await initializeDateFormatting('zh');
}

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  MyAppState createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return OKToast(
      dismissOtherOnShow: true,
//      child: buildMaterialApp(Colors.blue),
      child: StreamBuilder<MaterialColor>(
        stream: ColorRepo().themeColorStream,
        initialData: Colors.blue,
        builder: (context, snapshot) {
          var color = snapshot.data;
          OKTheme.themeColor = color;
          return buildMaterialApp(color);
        },
      ),
    );
  }

  MaterialApp buildMaterialApp(MaterialColor primaryColor) {
    return MaterialApp(
      title: 'OKGithub',
      theme: ThemeData(
        primarySwatch: primaryColor,
//        platform: TargetPlatform.android,
      ),
      home: LoginPage(),
//      routes: routes.routes,
      onGenerateRoute: (settings) {
//        print("onGenerateRoute settings = ${settings.name}");
        return routes.getRoute(settings);
      },
      navigatorObservers: [routes.observer],
//      initialRoute: "/login",
    );
  }
}
